//
//  RCTNotice.m
//  RCTNotice
//
//  Created by WTFSSD on 2018/1/23.
//  Copyright © 2018年 hubeihxkj. All rights reserved.
//

#import "RCTNotice.h"
#import <NetworkExtension/NetworkExtension.h>
#import "Reachability.h"
#import <UIKit/UIKit.h>
#import <React/RCTUtils.h>
@interface RCTNotice()

@property(nonatomic)RCTPromiseResolveBlock resolve;
@property(nonatomic)RCTPromiseRejectBlock reject;

@end


@implementation RCTNotice
RCT_EXPORT_MODULE();
RCT_EXPORT_METHOD(joinWifi:(NSDictionary*)params
                  resolver:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject){
    self.resolve = resolve;
    self.reject = reject;
    if(!params){
         self.reject(@"400",@"请设置正确的名称和密码",nil);
        return;
    }
//    if([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] !=ReachableViaWiFi){
//        [self alert:@"" message:@"您的wifi未打开是否现在打开?" onOk:^(id action) {
//            [self openWifiSetting];
//        }];
//        return;
//    }
    if (@available(iOS 11.0, *)) {
        NSString * ssid =params[@"ssid"];
        NSString * passphrase = params[@"passphrase"];
        BOOL isWEP = NO;
        if(!ssid) ssid = @"";
        if(!passphrase) passphrase = @"";
        if(params[@"isWEP"] != nil) isWEP =[params[@"isWEP"] boolValue];
        dispatch_async(dispatch_get_main_queue(), ^{
            NEHotspotConfigurationManager * manager = [NEHotspotConfigurationManager sharedManager];
            NEHotspotConfiguration * conf = [[NEHotspotConfiguration alloc] initWithSSID:ssid passphrase:passphrase isWEP:isWEP];
            [manager applyConfiguration:conf completionHandler:^(NSError * _Nullable error) {
                NSLog(@"%@",error);
                if(!error){
                    if(error.localizedDescription == @"already associated."){
                        self.resolve(@[@(YES),@"already associated"]);
                    }else{
                        self.reject(@"400",@"连接失败",nil);
                    }
                }
            }];
        });
    }else{
       self.reject(@"400",@"您当前系统版小于11.0",nil);
    }
}



-(void)alert:(NSString*)title message:(NSString*)message onOk:(void(^)(id))onOk{
    dispatch_block_t block = ^{
        UIAlertController * alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]];
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:onOk]];
         UIViewController *root = RCTPresentedViewController();
        [root presentViewController:alert animated:YES completion:nil];
    };

    dispatch_async(dispatch_get_main_queue(),block);
}

-(void)openWifiSetting{
    [self openSetting:@"WIFI"];
}

-(void)openSytenSetting:(NSString*)setting{
    dispatch_async(dispatch_get_main_queue(),^{
        
        if (@available(iOS 10.0, *)){
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:^(BOOL success) {
                
            }];
            
        }else{
            NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"prefs:root=%@",setting]];
            if([[UIApplication sharedApplication] canOpenURL:url]){
                [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
                
                }];
            }
        }
        
    });
}


RCT_EXPORT_METHOD(openSetting:(NSString*)setting){
    [self openSetting:setting];
}


@end
